<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>众阳健康</title>
    <meta name="description" content="众阳健康">
    <meta name="keywords" content="众阳健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script type="text/javascript"
            src="https://api.map.baidu.com/api?v=1.0&type=webgl&ak=p7tyhmFy70HujfA7y8U4n2eUaXGr637G"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }

        .container {
            overflow: hidden;
            width: 500px;
            height: 400px;
            margin: 0;
            font-family: "微软雅黑";
        }
        #container {
            overflow: hidden;
            width: 500px;
            height: 400px;
            margin: 0;
            font-family: "微软雅黑";
        }

    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>体检机构管理<small>体检机构地址管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>体检机构管理</el-breadcrumb-item>
            <el-breadcrumb-item>体检机构地址管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="体检机构名/体检机构地址" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage1()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="体检机构名称" align="center"></el-table-column>
                <el-table-column prop="telephone" label="联系电话" align="center"></el-table-column>
                <el-table-column prop="coordinates" label="经纬度" align="center"></el-table-column>
                <el-table-column label="机构图片" align="center">
                    <template slot-scope="scope">
                        <img width="100px" height="100px" :src="'http://qqh4s430z.hd-bkt.clouddn.com/'+scope.row.img"/>
                    </template>
                </el-table-column>
                <el-table-column prop="location" label="详细地址" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增体检机构" :visible.sync="dialogFormVisible" @opened="creatMap()">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="体检机构名" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="联系电话" prop="telephone">
                                    <el-input v-model="formData.telephone"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="上传体检机构地址图片">
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/address/upload.do"
                                            :auto-upload="autoUpload"
                                            name="imgFile"
                                            :show-file-list="false"
                                            :on-success="handleAvatarSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="经纬度">
                                    <el-input v-model="formData.coordinates" disabled="true" id="aaa" type="text"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="详细地址" prop="location">
                                    <el-input v-model="formData.location"  id="bbb"  type="text" style="width: 60%"></el-input>
                                    <el-button type="primary" icon="el-icon-location" @click="Crientation()" round>
                                        设置定位
                                    </el-button>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>

                    <!--<div style="display: none" ref="addContainer">-->
                    <div v-show="flag" >
                        <div class="container"></div>
                    </div>

                    <div slot="footer" class="dialog-footer">
                        <el-button @click=" dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑体检机构" :visible.sync="dialogFormVisible4Edit" @opened="creatEditMap()">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="体检机构名" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="联系电话" prop="telephone">
                                    <el-input v-model="formData.telephone"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="上传体检机构地址图片">
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/address/upload.do"
                                            :auto-upload="autoUpload"
                                            name="imgFile"
                                            :show-file-list="false"
                                            :on-success="handleAvatarSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="经纬度">
                                    <el-input v-model="formData.coordinates"  disable="true" type="text"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="详细地址" prop="location">
                                 <!--   <el-input v-model="formData.location" type="text"   @change="Crientation()" style="width: 60%"></el-input>-->
                                    <el-input v-model="formData.location" type="text" id="aaa"  disable="true"  style="width: 60%"></el-input>
                                    <el-button type="primary" icon="el-icon-location"  @click="Crientation()" round>设置定位</el-button>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div>
                        <div id="container"></div>
                    </div>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click=" Cancel()">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var checkMobile = (rule, value, cb) => {
        // 验证手机号的正则表达式
        const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
        if (regMobile.test(value)) {
            return cb();
        }
        cb(new Error("请输入合法的手机号"));
    };
    var vue = new Vue({
        el: '#app',
        data: {
            autoUpload: true,//自动上传
            imageUrl: null,//模型数据，用于上传图片完成后图片预览
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            flag:false,
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData: [],//添加表单窗口中检查组列表数据
            checkgroupIds: [],//添加表单窗口中检查组复选框对应id
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false,//控制编辑窗口显示/隐藏
            rules: {//校验规则
                telephone: [{validator: checkMobile, message: "请输入正确的手机号码", trigger: "blur"}, {
                    required: true,
                    message: '项目名称为必填项',
                    trigger: 'blur'
                }],
                name: [{required: true, message: '项目名称为必填项', trigger: 'blur'}],
                location: [{required: true, message: '详细地址为必填项', trigger: 'blur'}],
            }
        },
        created() {
            this.findPage();
        },
        methods: {
            creatMap() {
               let container= document.getElementsByClassName("container")[0];
                map = new BMapGL.Map(container);
                            // 创建地图实例
                point = new BMapGL.Point(116.404, 39.915);
                // 创建点坐标
                map.centerAndZoom(point, 15);
                map.enableScrollWheelZoom(true);
                locationControl = new BMapGL.LocationControl();  // 添加缩放控件
                map.addControl(locationControl);
                cityCtrl = new BMapGL.CityListControl();  // 添加城市列表控件
                map.addControl(cityCtrl);
                // 创建点标记
                marker = new BMapGL.Marker(point, {
                    enableDragging: true
                });
                map.addOverlay(marker);

               // 在地图上添加点标记
               //单击地图会把经纬度赋给 formData.coordinates
                map.addEventListener('click', function (e) {

                    vue.$data.formData.coordinates = e.latlng.lng + ',' + e.latlng.lat;
                    /*alert(window.document.getElementById("aaa").value);
                    alert('点击位置经纬度：' + e.latlng.lng + ',' + e.latlng.lat);*/
                     vue.$forceUpdate();

                });
                //单击地图会把详细地址赋给 formData.location
            /*    var geoc = new BMapGL.Geocoder();
                map.addEventListener('click', function (e) {
                    map.clearOverlays();
                    console.log(e);
                    var pt = e.latlng;
                    var marker = new BMapGL.Marker(new BMapGL.Point(pt.lng, pt.lat));
                    map.addOverlay(marker);
                    geoc.getLocation(pt, function (rs) {
                        var addComp = rs.addressComponents;
                        vue.$data.formData.location = addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber;
                    });
                         vue.$forceUpdate();

                })*/

            },
            creatEditMap() {
                var ss = this.formData.coordinates.split(",");
                map = new BMapGL.Map("container");              // 创建地图实例

                point = new BMapGL.Point(ss[0], ss[1]);
                // 创建点坐标
                map.centerAndZoom(point, 15);
                map.enableScrollWheelZoom(true);
                locationControl = new BMapGL.LocationControl();  // 添加缩放控件
                map.addControl(locationControl);
                cityCtrl = new BMapGL.CityListControl();  // 添加城市列表控件
                map.addControl(cityCtrl);

                marker = new BMapGL.Marker(point);
                // 在地图上添加点标记
                map.addOverlay(marker);
                // 创建信息窗口
                var opts = {
                    width: 200,
                    height: 100,
                    title: ''
                };
                var infoWindow = new BMapGL.InfoWindow(vue.$data.formData.location, opts);
                // 点标记添加点击事件
                marker.addEventListener('click', function () {
                    map.openInfoWindow(infoWindow, point); // 开启信息窗口
                });

                //单击地图会把经纬度赋给 formData.coordinates
                map.addEventListener('click', function (e) {
                     vue.$data.formData.coordinates = e.latlng.lng + ',' + e.latlng.lat;
                    /*alert(window.document.getElementById("aaa").value);
                    alert('点击位置经纬度：' + e.latlng.lng + ',' + e.latlng.lat);*/
                    /*alert("执行修改");*/
                     vue.$forceUpdate();

                });

            },

            setNewCenter(){
                var lng = 116.514 + Math.floor(Math.random() * 589828) / 1e6;
                var lat = 39.416 + Math.floor(Math.random() * 514923) / 1e6;
                var point = new BMapGL.Point(lng, lat);
                map.setCenter(point); // 设置地图中心点
            },
            getMapCenter() {
              /*  this.flag = true;
                this.Crientation();*/
            },
            Crientation(){
                this.flag = true;
              /* let serch = window.document.getElementById("aaa").value;*/
                let s = this.formData.location;
                var local = new BMapGL.LocalSearch(map, {
                    renderOptions: {map: map}
                });
                local.search(s);
                var marker = new BMapGL.Marker(this.formData.coordinates, {
                    enableDragging: true
                });
                map.addOverlay(marker);

                },
    //文件上传成功后的钩子，response为服务端返回的值，file为当前上传的文件封装成的js对象
    handleAvatarSuccess(response, file)
    {
        this.imageUrl = "http://qqh4s430z.hd-bkt.clouddn.com/" + response.data;
        this.$message({
            type: response.flag ? "success" : "error",
            message: response.message
        });
        this.formData.img = response.data;

    }
    ,
    //上传图片之前执行
    beforeAvatarUpload(file)
    {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isJPG) {
            this.$message.error('上传套餐图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
            this.$message.error('上传套餐图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
    }
    ,
    //添加
    handleAdd()
    {
        this.$refs['dataAddForm'].validate((valid) => {
            if (valid) {
                axios.post("/address/add.do", this.formData).then((resp) => {
                    this.dialogFormVisible = false;
                    if (resp.data.flag) {
                        this.$message({
                            type: "success",
                            message: resp.data.message
                        })

                    } else {
                        this.$message.error(resp.data.message)
                    }
                }).finally(() => {
                    this.findPage();
                })
            }
        })

    },
    //条件查询
    findPage1(){
        let param = {
            currentPage:1,
            pageSize: this.pagination.pageSize,
            queryString: this.pagination.queryString
        };
        axios.post("/address/findPage.do", param).then((resp) => {
            this.pagination.total = resp.data.total;
            this.dataList = resp.data.rows;
        })

    },
    //分页查询
    findPage()
    {
        let param = {
            currentPage: this.pagination.currentPage,
            pageSize: this.pagination.pageSize,
            queryString: this.pagination.queryString
        };
        axios.post("/address/findPage.do", param).then((resp) => {
            this.pagination.total = resp.data.total;
            this.dataList = resp.data.rows;
        })

    }
    ,
    // 重置表单
    resetForm()
    {
        this.formData = {};
        this.tableData = [];
        this.checkgroupIds = [];
        this.imageUrl = null;
        this.flag=false
    }
    ,
    // 弹出添加窗口
    handleCreate()
    {
        this.resetForm();
        this.dialogFormVisible = true;

    },

            Cancel(){
                this.dialogFormVisible4Edit = false;
               this.findPage();
            },

    // 弹出编辑窗口
    handleUpdate(row)
    {
        this.dialogFormVisible4Edit = true;
        this.formData = row;
    }
    ,
    //编辑
    handleEdit()
    {
        this.$refs['dataEditForm'].validate((valid) => {
            if (valid) {
                axios.post("/address/edit.do", this.formData).then((resp) => {
                    this.dialogFormVisible4Edit = false;
                    if (resp.data.flag) {
                        this.$message({
                            message: resp.data.message,
                            type: "success"
                        });
                    } else {
                        this.$message.error(resp.data.message)
                    }
                }).finally(() => {
                    this.findPage();
                })
            } else {
                this.$message.error("表单校验失败，请检查你输入的信息是否正确！");
                return false;
            }
        }).finally(()=>{
            this.findPage();
        })

    }
    ,
    //切换页码
    handleCurrentChange(currentPage)
    {
        this.currentPage = currentPage;
        this.findPage();
    }
    ,
    // 删除
    handleDelete(row)
    {
        this.$confirm("你确定要删除此条数据吗？", "提示", {
            type: 'warning'
        }).then(() =>
            axios.get("/address/delete.do?id=" + row.id).then((resp) => {
                if (resp.data.flag) {
                    this.$message({
                        message: resp.data.message,
                        type: "success"
                    });
                    this.findPage();
                } else {
                    this.$message.error(resp.data.message)
                }
            }).catch((r) => {
                this.showMessage(r);
            })
        ).catch(() =>
            this.$message({
                message: "操作已取消",
                type: "info"
            })
        )
    }
    }
    })
</script>
</html>

